package com.gin.no_gay_alliance.fragment.user.tab;

import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;

import com.gin.nga.bbscode.utils.BbsTagParser;
import com.gin.no_gay_alliance.customview.ContentWebViewClient;
import com.gin.no_gay_alliance.databinding.FragmentUserCenterTabUserSignatureBinding;
import com.gin.no_gay_alliance.utils.ImageUtils;
import com.gin.no_gay_alliance.viewmodel.UserInfoModel;

/**
 * 用户签名
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/7 12:14
 **/
public class UserSignatureTabFragment extends BaseUserCenterTabFragment<FragmentUserCenterTabUserSignatureBinding> {
    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        b.wv.setWebChromeClient(new WebChromeClient());
        b.wv.setWebViewClient(new ContentWebViewClient(getContext()));
        b.wv.loadNodes(BbsTagParser.parseContent(userInfoModel.signature));
        b.wv.getSettings().setBlockNetworkImage(false);
        b.wv.addJavascriptInterface(this, "android");

    }

    /**
     * 预览图片，暴露给WebView
     *
     * @param url url
     */
    @JavascriptInterface
    public void onClickImg(String url) {
        // 删除缩略图后缀，预览原图
        ui(() -> ImageUtils.preview(requireActivity(), ImageUtils.removeSuffix(url)));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        if (b != null) {
            b.wv.destroy();
        }
        super.onDestroy();
    }

    /**
     * 实例化
     *
     * @param userInfoModel 用户信息
     * @return Fragment
     */
    public static UserSignatureTabFragment newInstance(UserInfoModel userInfoModel) {
        UserSignatureTabFragment fragment = new UserSignatureTabFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_USER_INFO, userInfoModel);
        fragment.setArguments(args);
        return fragment;
    }
}
